package com.example.broadcastsender;

import androidx.appcompat.app.AppCompatActivity;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import android.content.ComponentName;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;

public class MainActivity extends AppCompatActivity {


    ServiceConnection serviceConnection;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //注册本地广播
        LocalBroadcastManager.getInstance(this).registerReceiver(new MyReceiver(), new IntentFilter("com.example.broadcastsender.ACION.NORMAL_BC"));

        //定义绑定连接
        serviceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {

            }

            @Override
            public void onServiceDisconnected(ComponentName componentName) {

            }
        };
    }

    //    本地广播
    public void sendload(View view) {
        Intent intent = new Intent("com.example.broadcastsender.ACION.NORMAL_BC");
        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    }

    //    有序广播
    public void sendOrder(View view) {

        Intent intent = new Intent("com.example.broadcastsender.ACION.NORMAL_BC");
        sendOrderedBroadcast(intent, null);

    }

    //    正常广播
    public void sendNormal(View view) {
        //隐式广播
        Intent intent = new Intent("com.example.broadcastsender.ACION.NORMAL_BC");
//        intent.setData()
//        intent.putExtra()
        //显式广播
//        intent.setPackage("com.example.broadcastreciver");
//        intent.setPackage("com.example.broadcastreciver2");
        sendBroadcast(intent);
    }

    public void startServer(View view) {
        Intent intent = new Intent(this, MyService.class);
        startService(intent);
    }

    public void stopServer(View view) {
        Intent intent = new Intent(this, MyService.class);
        stopService(intent);
    }

    public void boundServer(View view) {
        Intent intent = new Intent(this, MyService.class);
        bindService(intent, serviceConnection, BIND_AUTO_CREATE);
    }

    public void unboundServer(View view) {
        unbindService(serviceConnection);
    }
}